#**************************************************************************
#*********************---------------------------------**********************
#********************|    Copyright (c) 2003 - 2022    |*********************
#********************|      by VIRE Technologies.        |*********************
#********************|       All Rights Reserved           |*********************
#*********************---------------------------------**********************
#**************************************************************************
#
# If any question, please write to VIRE Technologies for
# more information:
#			Techart Plaza, A16, No.30 XueYuan Road,
#			Haidian District Beijing 100083,
#			P.R. China
#
# For more, please visit company website: www.vire.cn
#**************************************************************************

# Macros and configuration to support builds of Qt projects
# See Qt CMake manual for documentation (http://doc.qt.io/qt-5/cmake-manual.html)

# Qt meta object compiler (moc). Use AUTOMOC to automatically scan source files and invoke moc accordingly.
set(CMAKE_AUTOMOC ON)

# Qt resource compiler (rcc). Use AUTORCC to invoke rcc code generator for .qrc files added as target sources.
set(CMAKE_AUTORCC ON)

# Qt user interface compiler (uic). Cannot use AUTOUIC because .ui files need to be in same directory as source (until CMake 3.9)
#set(CMAKE_AUTOUIC ON)

# Automatically add the current source- and build directories to the include path. Useful for including .h files generated by uic.
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Group the automoc targets together in an IDE, e.g. in MSVS
set_property(GLOBAL PROPERTY AUTOGEN_TARGETS_FOLDER QtAutogenTargets)

# Group files generated by AUTOMOC and AUTORCC together in their own folders.
if (CMAKE_VERSION VERSION_GREATER 3.9)
   set_property(GLOBAL PROPERTY AUTOMOC_SOURCE_GROUP qt_autogen\\moc)
   set_property(GLOBAL PROPERTY AUTORCC_SOURCE_GROUP qt_autogen\\rcc)
endif()

# configure_qt_project([MODULES <modules>...]
#                      [UI <outfiles> <inputfile...>]
#                      [RCC <target> <inputfile...> [DESTINATION ...]])
# Optional arguments:
# MODULES
#     Specifies additional Qt modules (beyond Core, Gui, Widgets) to find
# UI
#     Specifies arguments to qt5_wrap_ui for generating header files with uic
# RCC
#     Specifies arguments to qt5_add_binary_resources for generating binary resources with rcc
macro(configure_qt_project)
   set(multiValueArgs MODULES UI RCC GEN_UI_HEADERS)
   cmake_parse_arguments(CONFIG_QT "" "" "${multiValueArgs}" ${ARGN})

    IF(EXISTS ${CONAN_QT_ROOT})
        SET(3RD_Qt_ROOT ${CONAN_QT_ROOT})
    ELSEIF(EXISTS ${PROJECT_QTDIR})
        SET(3RD_Qt_ROOT ${PROJECT_QTDIR})
    ELSE()
       
    ENDIF()

    # conan support
    SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_BINARY_DIR}/../generators)
    SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_BINARY_DIR})
    SET(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${CMAKE_BINARY_DIR})
    SET(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${CMAKE_BINARY_DIR}/../generators)

   # Find Qt5 package if not already found
   if (NOT Qt5_FOUND)
      # set(3RD_Qt_ROOT "" CACHE PATH "")
      if(NOT EXISTS ${3RD_Qt_ROOT})
         message(FATAL_ERROR "3RD_Qt_ROOT=${3RD_Qt_ROOT}")
      endif()
      find_package(Qt5 REQUIRED 
        COMPONENTS ${CONFIG_QT_MODULES} 
      )
      if (NOT Qt5_FOUND)
         SET(PROJECT_QTDIR "" CACHE PATH "Select Qt Root PATH")
         IF(EXISTS ${PROJECT_QTDIR})
            SET(3RD_Qt_ROOT ${PROJECT_QTDIR})
         ELSE()
            message(FATAL_ERROR "Please set PROJECT_QTDIR to the root of your Qt5 installation.")
         ENDIF()
         message(FATAL_ERROR "123123")

         find_package(Qt5 REQUIRED CONFIG 
            COMPONENTS ${CONFIG_QT_MODULES} 
            NO_DEFAULT_PATH 
            PATHS 
               ${3RD_Qt_ROOT}
         )
      endif()
   endif()

   # Group files generated by AUTOMOC and AUTORCC together in their own folders.
   # This can be removed once a minimum CMake 3.9 is required.
   if (CMAKE_VERSION VERSION_LESS 3.9)
      source_group(qt_autogen\\moc REGULAR_EXPRESSION "_automoc.cpp$")
      source_group(qt_autogen\\rcc REGULAR_EXPRESSION "qrc_.+\\.cpp$")
   endif()
   # Group files generated by uic together in a folder
   source_group(qt_autogen\\uic REGULAR_EXPRESSION "ui_.+\\.h$")

   # Group .ui and .qrc files together into a folder
   source_group(ui REGULAR_EXPRESSION ".+\\.ui$|.+\\.qrc$")

   # Find required Qt modules by luli:
   # 当在conan模式，QT 源码生成的cmake/lib没有 xxxConfig.cmake文件
   #if (CONFIG_QT_MODULES)
      #foreach(module ${CONFIG_QT_MODULES})
         #find_package(Qt5${module} REQUIRED)
      #endforeach()
   #endif()

   # Generate headers using qt5_wrap_ui (until AUTOUIC can be used)
   if (CONFIG_QT_UI)
      # 当在conan模式，QT 源码生成的cmake/lib没有 xxxConfig.cmake文件
      #if (NOT Qt5Widgets_FOUND)
         #find_package(Qt5Widgets REQUIRED)
      #endif()
      set(ui_files ${CONFIG_QT_UI})
      list(GET ui_files 0 UI_OUTFILES)
      list(REMOVE_AT ui_files 0)
      set(${UI_OUTFILES}) # Clear list of output files
      qt5_wrap_ui(${UI_OUTFILES} ${ui_files})
      set(${CONFIG_QT_GEN_UI_HEADERS} ${UI_OUTFILES})
      SET(${UI_OUTFILES} ${${UI_OUTFILES}} ${ui_files})
   endif()

   # Generate binary rcc file from qrc files using qt5_add_binary_resources
   if (CONFIG_QT_RCC)
      cmake_parse_arguments(RCC_ARGS "" "DESTINATION" "" ${CONFIG_QT_RCC})
      set(rcc_files ${RCC_ARGS_UNPARSED_ARGUMENTS})
      list(GET rcc_files 0 RCC_TARGET)
      list(REMOVE_AT rcc_files 0)
      qt5_add_binary_resources(${RCC_TARGET} ${rcc_files} DESTINATION ${RCC_ARGS_DESTINATION})
      get_property(AutogenTargetsFolder GLOBAL PROPERTY AUTOGEN_TARGETS_FOLDER)
      set_property(TARGET ${RCC_TARGET} PROPERTY FOLDER ${AutogenTargetsFolder})
   endif()
endmacro()

macro(configure_qt_conf TARGET_NAME)
   add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E echo "[Paths]" > $<TARGET_FILE_DIR:${TARGET_NAME}>/qt.conf
      COMMAND ${CMAKE_COMMAND} -E echo "Prefix = ${QTROOT}" >> $<TARGET_FILE_DIR:${TARGET_NAME}>/qt.conf
      COMMENT "Configuring qt.conf for ${TARGET_NAME}"
   )
   add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E echo "[Paths]" > $<TARGET_FILE_DIR:${TARGET_NAME}>/qt.conf.install
      COMMAND ${CMAKE_COMMAND} -E echo "Plugins = qt_plugins" >> $<TARGET_FILE_DIR:${TARGET_NAME}>/qt.conf.install
   )
   install(FILES $<TARGET_FILE_DIR:${TARGET_NAME}>/qt.conf.install DESTINATION bin RENAME qt.conf COMPONENT Runtime)
endmacro()

function(QT5_WRAP_UITT outfiles )
    set(options)
    set(oneValueArgs)
    set(multiValueArgs OPTIONS)

    cmake_parse_arguments(_WRAP_UI "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    set(ui_files ${_WRAP_UI_UNPARSED_ARGUMENTS})
    set(ui_options ${_WRAP_UI_OPTIONS})
   #message(FATAL_ERROR ${Qt5Widgets_UIC_EXECUTABLE})
    foreach(it ${ui_files})
        get_filename_component(outfile ${it} NAME_WE)
        get_filename_component(infile ${it} ABSOLUTE)
        get_filename_component(dir ${it} PATH)
        set(outfile ${CMAKE_CURRENT_BINARY_DIR}/ui_${outfile}.h)
        execute_process(COMMAND ${Qt5Widgets_UIC_EXECUTABLE}
            ${ui_options} -o ${outfile} ${infile}
            WORKING_DIRECTORY ${dir}
         )
      #   add_custom_command(OUTPUT ${outfile}
      #     COMMAND ${Qt5Widgets_UIC_EXECUTABLE}
      #     ARGS ${ui_options} -o ${outfile} ${infile}
      #     MAIN_DEPENDENCY ${infile} VERBATIM)
        set_source_files_properties(${infile} PROPERTIES SKIP_AUTOUIC OFF)
        set_source_files_properties(${outfile} PROPERTIES SKIP_AUTOMOC ON)
        set_source_files_properties(${outfile} PROPERTIES SKIP_AUTOUIC ON)
        list(APPEND ${outfiles} ${outfile})
    endforeach()
    set(${outfiles} ${${outfiles}} PARENT_SCOPE)
endfunction()